package com.lant.web.controller.user;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.lant.enums.ResponseStatusEnum;
import com.lant.pojo.LantResult;
import com.lant.web.ao.UserAO;
import com.lant.web.dto.LoginDTO;
import com.lant.web.dto.RegisterDTO;

@RestController
@RequestMapping("/user")
public class UserController {

    private static final Logger logger = LoggerFactory.getLogger(UserController.class);

    @Autowired
    private UserAO userAO;

    /**
     * @Title: login
     * @Description: 用户登录
     * @param loginDTO 登录参数
     * @param request
     * @param response
     * @return LantResult 返回类型
     * @author YFL
     */
    @PostMapping("/login")
    public LantResult login(LoginDTO loginDTO, HttpServletRequest request, HttpServletResponse response) {
        try {
            return userAO.userLogin(loginDTO, request, response);
        } catch (Exception e) {
            logger.error("用户登录失败:{}", e.getMessage());
            return LantResult.build(ResponseStatusEnum.BAD_REQUEST.getValue(), e.getMessage());
        }
    }

    /**
     * @Title: save
     * @Description: 用户注册
     * @param: registerDTO
     * @return: LantResult
     * @author: YFL
     * @date: 2020年5月20日 下午1:50:56
     */
    @PostMapping("/save")
    public LantResult save(RegisterDTO registerDTO) {
        try {
            return userAO.createUser(registerDTO);
        } catch (Exception e) {
            logger.error("用户注册失败:{}", e.getMessage());
            return LantResult.build(ResponseStatusEnum.BAD_REQUEST.getValue(), e.getMessage());
        }
    }

    /**
     * @Title: checkData
     * @Description: 检测用户数据
     * @param content
     * @param: type
     * @return: LantResult
     * @author: YFL
     * @date: 2020年6月4日 下午2:03:57
     */
    @PostMapping("/checkData")
    public LantResult checkData(String content, Integer type) {
        try {
            return userAO.checkData(content, type);
        } catch (Exception e) {
            logger.error("检测用户数据失败:{}", e.getMessage());
            return LantResult.build(ResponseStatusEnum.BAD_REQUEST.getValue(), e.getMessage());
        }
    }
}
